package org.linlinjava.litemall.admin.web2.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req4;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req5;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req6;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req7;
import org.linlinjava.litemall.core.system.SystemStaticParam;
import org.linlinjava.litemall.core.util.R;
import org.linlinjava.litemall.core.util.Util;
import org.linlinjava.litemall.db.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统设置
 */
@Api("系统设置[发货时效设置、覆盖范围设置、提现设置和加盟店]")
@RestController
@RequestMapping("/system/base")
public class SystemBaseController {

    private static Logger log = LoggerFactory.getLogger(SystemBaseController.class);

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "发货时效设置页面数据")
    @GetMapping("/index1")
    public Object index1() {
        Map<String, Object> map = Util.hMap();
        map.put("normalType", redisUtil.getInt(SystemStaticParam.normalBuySendTime));
        map.put("time1", redisUtil.get(SystemStaticParam.time1));
        map.put("time2", redisUtil.get(SystemStaticParam.time2));
        map.put("time3", redisUtil.get(SystemStaticParam.time3));
        map.put("groupBuySendTime", redisUtil.getInt(SystemStaticParam.groupBuySendTime));
        return R.ok(map);
    }

    @ApiOperation(value = "保存发货时效设置页面数据")
    @PostMapping("/save1")
    public Object save1(@RequestBody Req4 req4) {
        redisUtil.set(SystemStaticParam.normalBuySendTime, req4.getNormalType());
        redisUtil.set(SystemStaticParam.time1, req4.getTime1());
        redisUtil.set(SystemStaticParam.time2, req4.getTime2());
        redisUtil.set(SystemStaticParam.time3, req4.getTime3());
        redisUtil.set(SystemStaticParam.groupBuySendTime, req4.getGroupBuySendTime());
        return R.ok();
    }

    @ApiOperation(value = "覆盖范围设置页面数据")
    @GetMapping("/index2")
    public Object index2() {
        Map<String, Object> map = Util.hMap();
        map.put("group1", redisUtil.get(SystemStaticParam.group1));
        map.put("group2", redisUtil.get(SystemStaticParam.group2));
        map.put("group3", redisUtil.get(SystemStaticParam.group3));
        map.put("group4", redisUtil.get(SystemStaticParam.group4));
        return R.ok(map);
    }

    @ApiOperation(value = "保存覆盖范围设置页面数据")
    @PostMapping("/save2")
    public Object save2(@RequestBody Req5 req) {
        redisUtil.set(SystemStaticParam.group1, req.getGroup1());
        redisUtil.set(SystemStaticParam.group2, req.getGroup2());
        redisUtil.set(SystemStaticParam.group3, req.getGroup3());
        redisUtil.set(SystemStaticParam.group4, req.getGroup4());
        return R.ok();
    }

    @ApiOperation(value = "提现设置页面数据")
    @GetMapping("/index3")
    public Object index3() {
        Map<String, Object> map = Util.hMap();
        map.put("cashOut1", redisUtil.get(SystemStaticParam.cashOut1));
        map.put("cashOut2", redisUtil.get(SystemStaticParam.cashOut2));
        map.put("cashOut3", redisUtil.get(SystemStaticParam.cashOut3));
        return R.ok(map);
    }

    @ApiOperation(value = "保存提现设置页面数据")
    @PostMapping("/save3")
    public Object save3(@RequestBody Req6 req) {
        redisUtil.set(SystemStaticParam.cashOut1, req.getCashOut1());
        redisUtil.set(SystemStaticParam.cashOut2, req.getCashOut2());
        redisUtil.set(SystemStaticParam.cashOut3, req.getCashOut3());
        return R.ok();
    }

    @ApiOperation(value = "加盟店申请设置页面数据",notes = "0-开放申请、1-隐藏申请")
    @GetMapping("/index4")
    public Object index4() {
        Map<String, Object> map = Util.hMap();
        map.put("shopApplyIsOpen", redisUtil.get(SystemStaticParam.shopApplyIsOpen));

        map.put("minCommission", redisUtil.getInt(SystemStaticParam.minCommission));
        map.put("maxCommission", redisUtil.getInt(SystemStaticParam.maxCommission));

        map.put("minDisCount", redisUtil.getDouble(SystemStaticParam.minDisCount));
        map.put("maxDisCount", redisUtil.getDouble(SystemStaticParam.maxDisCount));
        return R.ok(map);
    }

    @ApiOperation(value = "保存加盟店申请设置页面数据")
    @PostMapping("/save4")
    public Object save4(@RequestBody Req7 req) {
        redisUtil.set(SystemStaticParam.shopApplyIsOpen, req.getShopApplyIsOpen());

        redisUtil.set(SystemStaticParam.minDisCount, req.getMinDisCount());
        redisUtil.set(SystemStaticParam.maxDisCount, req.getMaxDisCount());

        redisUtil.set(SystemStaticParam.minCommission, req.getMinCommission());
        redisUtil.set(SystemStaticParam.maxCommission, req.getMaxCommission());
        return R.ok();
    }
}
